<?php
namespace RMD\AccountBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Length;

class Subscriber
{
	private $id;
	private $lists_id;
	private $firstname;
	private $lastname;
	private $mailname;
	private $servername;
	private $description;
	private $signedfrom;
	private $dateSigned;
	private $mailType;
	private $status;
	
	private $email = null;
	
	protected $list;
	protected $groups;
	protected $groupz;

	public function __construct() {
		$this->groups = new ArrayCollection();
		$this->signedfrom = 0;
		$this->mailType = 0;
		$this->status = 1;
	}
	
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        //$metadata->addConstraint(new UniqueEntity(array('fields' => array('email'))));
        
        $metadata->addPropertyConstraint('firstname', new Length(array("max" => 50)));
        $metadata->addPropertyConstraint('lastname', new Length(array("max" => 50)));
		
        $metadata->addPropertyConstraint('email', new NotBlank());
        $metadata->addPropertyConstraint('email', new Length(array("max" => 101)));
    }
	
	public function setEmail($email){
		$this->email = $email;
		$this->mailname = stristr($email, '@', true);
		$this->servername = substr(strrchr($email, '@'), 1 );
	}
	
	public function getEmail(){
		if (!$this->email) {
			$this->email = $this->mailname . '@' . $this->servername;
		}
		return $this->email;	
	}

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set lists_id
     *
     * @param integer $listsId
     * @return Subscriber
     */
    public function setListsId($listsId)
    {
        $this->lists_id = $listsId;
    
        return $this;
    }

    /**
     * Get lists_id
     *
     * @return integer 
     */
    public function getListsId()
    {
        return $this->lists_id;
    }

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return Subscriber
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
    
        return $this;
    }

    /**
     * Get firstname
     *
     * @return string 
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set lastname
     *
     * @param string $lastname
     * @return Subscriber
     */
    public function setLastname($lastname)
    {
        $this->lastname = $lastname;
    
        return $this;
    }

    /**
     * Get lastname
     *
     * @return string 
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * Set mailname
     *
     * @param string $mailname
     * @return Subscriber
     */
    public function setMailname($mailname)
    {
        $this->mailname = $mailname;
    
        return $this;
    }

    /**
     * Get mailname
     *
     * @return string 
     */
    public function getMailname()
    {
        return $this->mailname;
    }

    /**
     * Set servername
     *
     * @param string $servername
     * @return Subscriber
     */
    public function setServername($servername)
    {
        $this->servername = $servername;
    
        return $this;
    }

    /**
     * Get servername
     *
     * @return string 
     */
    public function getServername()
    {
        return $this->servername;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Subscriber
     */
    public function setDescription($description)
    {
        $this->description = $description;
    
        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set signedfrom
     *
     * @param integer $signedfrom
     * @return Subscriber
     */
    public function setSignedfrom($signedfrom)
    {
        $this->signedfrom = $signedfrom;
    
        return $this;
    }

    /**
     * Get signedfrom
     *
     * @return integer 
     */
    public function getSignedfrom()
    {
        return $this->signedfrom;
    }

    /**
     * Set dateSigned
     *
     * @param \DateTime $dateSigned
     * @return Subscriber
     */
    public function setDateSigned($dateSigned)
    {
        $this->dateSigned = $dateSigned;
    
        return $this;
    }

    /**
     * Get dateSigned
     *
     * @return \DateTime 
     */
    public function getDateSigned()
    {
        return $this->dateSigned;
    }

    /**
     * Set mailType
     *
     * @param integer $mailType
     * @return Subscriber
     */
    public function setMailType($mailType)
    {
        $this->mailType = $mailType;
    
        return $this;
    }

    /**
     * Get mailType
     *
     * @return integer 
     */
    public function getMailType()
    {
        return $this->mailType;
    }

    /**
     * Set status
     *
     * @param integer $status
     * @return Subscriber
     */
    public function setStatus($status)
    {
        $this->status = $status;
    
        return $this;
    }

    /**
     * Get status
     *
     * @return integer 
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set list
     *
     * @param \RMD\AccountBundle\Entity\Lists $list
     * @return Subscriber
     */
    public function setList(\RMD\AccountBundle\Entity\Lists $list = null)
    {
        $this->list = $list;
    
        return $this;
    }

    /**
     * Get list
     *
     * @return \RMD\AccountBundle\Entity\Lists 
     */
    public function getList()
    {
        return $this->list;
    }

    /**
     * Add groups
     *
     * @param \RMD\AccountBundle\Entity\Group $groups
     * @return Subscriber
     */
    public function addGroup(\RMD\AccountBundle\Entity\Group $group)
    {
		if ($this->isInGroup($group)) {
			return $this;
		}
        $this->groups[] = $group;
    
        return $this;
    }

    /**
     * Remove groups
     *
     * @param \RMD\AccountBundle\Entity\Group $groups
     */
    public function removeGroup(\RMD\AccountBundle\Entity\Group $groups)
    {
        $this->groups->removeElement($groups);
    }

    /**
     * Get groups
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGroups()
    {
        return $this->groups;
    }
	public function isInGroup(\RMD\AccountBundle\Entity\Group $group){
		$groupIds = array();
		foreach ($this->groups as $g) {
			$groupIds[] = $g->getId();
		}
		if (in_array($group->getId(), $groupIds)) {
			return true;
		}
		return false;
	}
}